layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'table', 'laydate'], function () {
    var form = layui.form,
        table = layui.table,
        laydate = layui.laydate,
        admin = layui.admin;

    //执行一个laydate实例
    laydate.render({
        elem: '#startTime' //指定元素
        ,trigger:'click'
    });
    var date = new Date();
    var year = date.getFullYear(); //获取完整的年份(4位)
    var month = date.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    var day = date.getDate(); //获取当前日(1-31)
    $("#startTime").val(year + "-" + month + "-" + day);

    table.render({
        elem: '#order_table'
        , url: '/order/queryData'
        , method: "post"
        , height: 500
        , limit: 20							//显示数目
        , limits: [20, 100]
        , cols: [[
            {field: 'id', title: '订单编号', width: 100, "align": "center"}
            , {field: 'storeId', width: 150, title: '门店ID', "align": "center"}
            , {
                field: 'totalFee', width: 150, title: '金额/元', "align": "center", templet: function (d) {
                    return new Number(d.totalFee).div(new Number(100));
                }
            }, {
                field: 'actFee', width: 150, title: '(实收)金额/元', "align": "center", templet: function (d) {
                    return new Number(d.actFee).div(new Number(100));
                }
            },
            {
                field: 'actInnerFee', width: 150, title: '(实到)金额/元', "align": "center", templet: function (d) {
                    return new Number(d.actInnerFee).div(new Number(100));
                }
            }
            ,
            {
                field: 'serviceFee', width: 150, title: '(服务费)金额/元', "align": "center", templet: function (d) {
                    return new Number(d.serviceFee).div(new Number(100));
                }
            },
            {
                field: 'discountMoney', width: 150, title: '(用户优惠)金额/元', "align": "center", templet: function (d) {
                    return new Number(d.discountMoney).div(new Number(100));
                }
            }
            , {
                field: 'type', width: 150, title: '支付类型', "align": "center", templet: function (d) {
                    if (d.type == 'WECHAT') {
                        return '微信';
                    } else if (d.type == 'ALIPAY') {
                        return '支付宝';
                    } else {
                        return "";
                    }
                }
            }
            , {
                field: 'payType', width: 150, title: '支付方式', "align": "center", templet: function (d) {
                    if (d.payType == 'FACE') {
                        return '刷脸';
                    } else if (d.payType == 'SQR') {
                        return '扫码';
                    } else if (d.payType == 'QRCODE') {
                        return '二维码';
                    } else {
                        return '';
                    }
                }
            }
            , {field: 'createdTime', width: 150, title: '创建时间', "align": "center"}
            , {
                field: 'status', width: 150, title: '状态', "align": "center", templet: function (d) {
                    if (d.status == 1) {
                        return "付款成功";
                    } else if (d.status == 0) {
                        return "未付款";
                    } else if (d.status == -1) {
                        return "支付失败";
                    } else if (d.status == -2) {
                        return "下单失败";
                    } else if (d.status == -3) {
                        return "交易取消";
                    } else if (d.status == -4) {
                        return "交易关闭";
                    } else if (d.status == -5) {
                        return "转入退款";
                    } else {
                        return "";
                    }
                }
            }
        ]]
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where: {
            startTime: $(".transaction_search input[name='startTime']").val(),
        }
        , id: 'order_table'
        , page: true
    });

    $(".search_btn").click(function () {

        table.reload('order_table', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                startTime: $(".transaction_search input[name='startTime']").val(),
                type: $(".transaction_search select[name='type']").val(),
                payType: $(".transaction_search select[name='payType']").val(),
                storeId: $(".transaction_search select[name='storeName']").val(),
                status: $(".transaction_search select[name='status']").val(),
            }
        });

    });

    //门店下拉框
    $.ajax({
        url: "/enterprise_store/queryData",
        type: "post",
        success: function (result) {
            if (result.code === 1) {
                $("#storeName").empty();
                var data = result.data;
                $("#storeName").append('<option value="">请选择门店</option>');
                for (var i = 0; i < data.length; i++) {
                    if (data[i].name != null) {
                        $("#storeName").append('<option value="' + data[i].id + '">' + data[i].name + '</option>');
                    }
                }
                form.render('select');
            } else {
                layer.msg(result.message, {
                    offset: '32%'
                    , icon: 2
                    , time: 3000
                }, function () {

                })
            }
        }
    });

});

function transactionExport(a) {
    var startTime = $(".transaction_search input[name='startTime']").val();
    var type = $(".transaction_search select[name='type']").val();
    var payType = $(".transaction_search select[name='payType']").val();
    var storeId = $(".transaction_search select[name='storeName']").val();
    var status = $(".transaction_search select[name='status']").val();

    if(startTime.length == 0 || startTime == null || startTime == undefined){
        alert("下载时间不能为空!");
        return;
    }

    var date = new Date();
    var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
    var month = date.getMonth() + 1; //获取当前月份(0-11,0代表1月)         // 所以获取当前月份是myDate.getMonth()+1;
    var day = date.getDate(); //获取当前日(1-31)
    var nowDate = year + "-" + month + "-" + day;
    if(!compareToDate(nowDate,startTime)){
        alert("查询时间不能大于当前时间!");
        a.href = "";
        return;
    }else{
        a.href = '/order/exportOrderInfo?startTime=' + startTime + "&type=" + type + "&payType=" + payType + "&storeId=" + storeId + "&status=" +status;
    }

}

function compareToDate(a, b) {
    var arr = a.split("-");
    var startTime = new Date(arr[0], arr[1], arr[2]);
    var startTimes = startTime.getTime();
    var arrs = b.split("-");
    var endTime = new Date(arrs[0], arrs[1], arrs[2]);
    var endTimes = endTime.getTime();
    // 进行日期比较
    if (startTimes >= endTimes) {
        return true;
    }else{
        return false;
    }
}
